package com.itheima.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter(filterName = "LoginFilter",urlPatterns = "/loginStudentServlet")
public class LoginFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        //获取登录页面参数
        HttpServletRequest reqest = (HttpServletRequest) req;
        String username = (String) reqest.getSession().getAttribute("username");
        System.out.println(132465);
        System.out.println(username);
        if (username == "" || username == null) {
//            PrintWriter pw = resp.getWriter();
            System.out.println(456);
//            pw.write("用户名为空，3秒钟跳转到登陆页面。。。");
//            HttpResponse resp1 = (HttpResponse) resp;
            HttpServletResponse response = (HttpServletResponse) resp;
//            response.setHeader("Refresh","3;URL=/day5/login.jsp");
            response.sendRedirect(reqest.getContextPath()+"/login.jsp");

            return;
        }

        chain.doFilter(req, resp);
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
